import matplotlib.pyplot as plt
import numpy as np

# 生成超长数据
x = np.linspace(0, 10000, 100000)
y = np.sin(x/100) + np.random.normal(0, 0.1, 100000)

from matplotlib.widgets import Slider

fig, ax = plt.subplots(figsize=(12, 6))
plt.subplots_adjust(bottom=0.25)  # 为滑块留出空间

line, = ax.plot(x, y, linewidth=0.5)
ax.set_title("可滚动的超长图表")
ax.grid(True)

# 创建滑块
ax_slider = plt.axes([0.2, 0.1, 0.6, 0.03])
slider = Slider(ax_slider, '位置', 0, len(x), valinit=0, valstep=100)

def update(val):
    pos = int(slider.val)
    window_size = 1000
    ax.set_xlim(pos, pos + window_size)
    fig.canvas.draw_idle()

slider.on_changed(update)
plt.show()


import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=x,
    y=y,
    mode='lines',
    line=dict(width=0.5)
))

fig.update_layout(
    title="超长图表 (Plotly)",
    xaxis_title="X轴",
    yaxis_title="Y轴",
    hovermode="x unified",
    height=500
)

# 添加滚动条
fig.update_layout(
    xaxis=dict(
        rangeslider=dict(visible=True),
        type="linear"
    )
)

fig.show()